window.onresize = layout;

function layout() {
	/* Spacing between the blocks ... */
	var margin_h=10;
	var margin_r=0;

	/* Shortcuts for the blocks */
	var pb=$('#pb');
	var m=$('#m');
	var mu = $('#mu');
	var b=$('#b');
	var h=$('#h');
	var r=$('#r');

	/* Some variables for convenience */
	var pb_w = pb.width();
	var pb_h = pb.height();
	var m_h = m.outerHeight();
	var mu_w = mu.outerWidth();

	var r_w = 0;
	if ( r.css('display') != 'none') {
		r_w= r.outerWidth();
		margin_r = 10;
	}
	var h_wb = h.outerWidth() - h.width();
	var h_hb = h.outerHeight() - h.height();

	/* Laying out elements */
	m.css('right','0px');
	mu.css('left',(pb_w-r_w-mu_w)/2 + 'px');
	
	h.css('left',Math.max((pb_w - r_w - margin_r - h.outerWidth())/2,1) + 'px');
	if (b.css('display')!='none') {
		var docb=document.getElementById('b');
		var b_sbw = docb.scrollWidth - docb.clientWidth;
		var b_wb = b.outerWidth() - b.width();
		var b_hb = b.outerHeight() - b.height();

		h.css('top',m_h + margin_h + 'px'); 

		b.css('top',(h.outerHeight() + h.position().top + margin_h) + 'px');
		b.css('max-height',(pb_h - b.position().top - margin_h - b_hb ) + 'px');

		/* Compensate for vertical scrollbar width if it suddenly appears */
		if ( b.outerWidth() < pb_w - r_w - margin_r - b_sbw && b_sbw ) {
			b.css('width',b.width()+b_sbw+1);
		}

		b.css('max-width',(pb_w - r_w - margin_r - b_wb) + 'px');
		b.css('left',Math.max((pb_w - r_w - margin_r - b.outerWidth())/2,1) + 'px');
	} else {
		h.css('top',((pb_h-m_h-margin_h-h.outerHeight())/2-40)+'px');
	}
	$('.menuitem').mouseover (function () {
		if (! $(this).hasClass("m-selected") ) {
			$(this).removeClass('m-out');
			$(this).addClass('m-over');
		}
	});
	$('.menuitem').mouseout (function () {
		if (! $(this).hasClass("m-selected") ) {
			$(this).removeClass('m-over');
			$(this).addClass('m-out');
		}
	});
	$('span.submenu').mouseover (function () {
		$(this).children('div').css('display','block');
	});
	$('span.submenu').mouseout (function () {
		$(this).children('div').css('display','none');
	});
}
function popup (content, event) {
	var p = $('#p');
	var bh = $('#pb').height();
	var bw = $('#pb').width();
	p.empty();
	p.css('max-height',(-100+bh)+'px');
	p.append(content);
	var close =$('<div class="menuitem m-out" style="margin-top:8px;">close</div>');
	p.append(close);

	var ph = p.outerHeight();
	var pw = p.outerWidth();
	var x; var y;
	if (event) {
		event.stopPropagation();
		x = Math.max(event.pageX - pw/2,0);
		if (x+pw>bw) { x=bw-pw-5; }
		y = Math.max(event.pageY - ph/2,0);
		if (y+ph>bh) { y=bh-ph-5; }
	} else {
		x = (bw-pw)/2;
		y = (bh-ph)/2;
	}
	p.css('top',y);
	p.css('left',x);
	p.css('display','block');
	layout();

	close.click(function (e) {
		e.stopPropagation();
		p.css('display','none');
	});
	p.click(function(e) {
		e.stopPropagation();
	});
/*	$(document).bind('click.popup',function(e) {
		p.css('display','none');
		$(document).unbind('click.popup');
	}); */
}
function harmonica (elem, title, level) {
	$(elem).addClass ('h-box');
	$(elem).addClass ('h-close');

	$(elem).children (':first').data('h-display',$(elem).children(':first').css('display'));
	$(elem).children (':first').css('display','none');
	$(elem).children (':first').addClass('h-level-'+level);

	var hdr = $('<div class="h-hdr icon-h-close" data-h-level="'+level+'">'+title+'</div>');
	$(elem).prepend(hdr);

	hdr.click (function() {
		if ($(this).next().css('display')!='none') {
			$(this).next().css('display','none');
			$(this).parent().removeClass('h-open');
			$(this).removeClass('icon-h-open');
			$(this).parent().addClass('h-close');
			$(this).addClass('icon-h-close');
		} else {
			var level = $(this).data('h-level');
			$('.h-level-'+level).css('display','none');
			$('.h-level-'+level).parent().removeClass('h-open');
			$('.h-level-'+level).prev().removeClass('icon-h-open');
			$('.h-level-'+level).parent().addClass('h-close');
			$('.h-level-'+level).prev().addClass('icon-h-close');
			$(this).next().css('display','table'); /* $(this).next().data('h-display')); */
			$(this).parent().removeClass('h-close');
			$(this).removeClass('icon-h-close');
			$(this).parent().addClass('h-open');
			$(this).addClass('icon-h-open');
		}
		layout();
	});
}
	
/* Delete element from array */
var conf;
function arrayDel(arr, str) {
	var cnt=$.inArray($.trim(str),arr);
	if (cnt!=-1) {
		arr.splice(cnt,1);
	}
}
/* to let plugins that have a configuration register */
var conf_registered = new Object();
function conf_Register (dataset, fn) {
	conf_registered[dataset]=fn;
}

/* get JSON data via post request  */
function postData (action, callback, request) {
	if (! request ) { request = new Object(); }
	$('#pb').css('cursor', 'progress');

	request.action=action;

	$.ajax ({type:'POST', url:'/DataProvider',data: JSON.stringify(request), contentType:'application/json', success:callback}); 
}

/* get new page via post request */
function postPage (action, param) {
	$('body').append ($('<form id="postPage" method="POST" action="/teamtools"></form>'));
	$('#postPage').append ($('<input type="hidden" name="action" value="'+action+'">'));
	if (param) {
		for (var i=0; i< param.length; i++) {
			var field = $('<input></input>');

			field.attr("type", "hidden");
			field.attr("name", param[i][0]);
			field.attr("value", param[i][1]);

			$("#postPage").append(field);
		}
	}
	$("#postPage").submit();

	return false;
}
function extTable_del (elem,data,del) {
	var e=$(elem);
	while ($(e).data('rownum') === undefined) {
		e=$(e).parent();
	}
	var rownum = $(e).data('rownum');
	delete data[rownum];
	$(e).remove();
	if (typeof del == "function") {
		del(rownum);
	}
}
function extTable_add (tbl, row, data, del) {
	var img = $('<img src="/img/menu-minus.gif" style="cursor:pointer;">');
	$(row).prepend ($('<td with="15px;"></td>').append(img)); 
	$(tbl).children('tbody').children ('tr:last').before (row);
	$(img).click(function () {
		extTable_del(this, data, del);
	});
}
function extTable (tbl, data, add, del) {
	
	var rows = $(tbl).children('tbody').children('tr');
	for (var i=0; i<rows.length-1; i++) {
		if ($(rows[i]).attr('data-rownum') !== undefined) {
			var min = $('<img src="/img/menu-minus.gif" style="cursor:pointer;">');
			$(rows[i]).prepend ($('<td with="15px;"></td>').append(min));
			$(min).click(function() {
				extTable_del(this, data, del);
			});
		} else {
			var dsp = $(rows[i]).children('td:first,th:first');
			if ($(dsp).attr('colspan') !== undefined) {
				$(dsp).attr('colspan',parseInt($(dsp).attr('colspan'),10)+1);
			} else {
				$(dsp).attr('colspan','2');
			}
		}
	}

	var plus = $('<img src="/img/menu-plus.gif" style="cursor:pointer;">');
	$(tbl).children('tbody').children('tr:last').prepend($('<td width="15px;"></td>').append(plus));
	$(plus).click(function () {
		extTable_add(tbl,add(), data, del);
	});
}
function printPage() {
	var winref=window.open();
	winref.document.write ('<html><head></head><body><div id="printref"></div></body></html>');
	winref.document.close ();

	$('#printref',winref.parent.document).append ($('#b').html());

	var a = $('head').children ('style');
alert (a.length);
	for (var i=0; i<a.length;i++) {
		alert ($(a[i]).text());
	}

	return false;
}
